package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.dto.OldForNewProductDto;
import com.ytjj.qmyx.mall.model.dto.RecordSubsidyDataDto;
import com.ytjj.qmyx.mall.model.response.*;
import com.ytjj.qmyx.mall.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 以旧换新
 *
 **/
@RestController
@RequestMapping("/h5/oldfornew")
public class H5OldForNewController extends BaseController {
    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;

    @GetMapping("account")
    public CommonResult account () {
        OldForNewAccountResponse oldForNewAccountResponse=new OldForNewAccountResponse();
        String recordKey = RedisConstants.RECORD_SUBSIDYKEY + TokenUtil.getToken();
        String recordDto = redisService.get(recordKey);
        if (StringUtils.isNotEmpty(recordDto)) {
            RecordSubsidyDataDto recordSubsidyDataDto = JSONObject.parseObject(recordDto, RecordSubsidyDataDto.class);
            recordSubsidyDataDto.setReceived(Optional.ofNullable(recordSubsidyDataDto.getReceived()).orElse(BigDecimal.ZERO).setScale(0, BigDecimal.ROUND_HALF_UP));
            recordSubsidyDataDto.setUsed(Optional.ofNullable(recordSubsidyDataDto.getUsed()).orElse(BigDecimal.ZERO).setScale(0, BigDecimal.ROUND_HALF_UP));
            oldForNewAccountResponse.setUsed(recordSubsidyDataDto.getUsed());
            oldForNewAccountResponse.setNotUsed(recordSubsidyDataDto.getReceived().subtract(recordSubsidyDataDto.getUsed()));
        }
        return CommonResult.success(oldForNewAccountResponse);
    }
    @GetMapping("index")
    public CommonResult index () {
        H5OldForNewIndexResponse oldForNewIndexResponse=new H5OldForNewIndexResponse();
        //推荐领取
        oldForNewIndexResponse.setRecommendProductList(activityService.getList(1,30, 14));
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        //大家电
        OldForNewColumnResponse oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(41);
        oldForNewColumnResponse.setTitle("大家电");
        oldForNewColumnResponse.setProductList(activityService.getList(1,6, 41));
        columnResponseList.add(oldForNewColumnResponse);
        //生活电器
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(42);
        oldForNewColumnResponse.setTitle("生活电器");
        oldForNewColumnResponse.setProductList(activityService.getList(1,6, 42));
        columnResponseList.add(oldForNewColumnResponse);
        //厨卫家居
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(43);
        oldForNewColumnResponse.setTitle("厨卫家居");
        oldForNewColumnResponse.setProductList(activityService.getList(1,6, 43));
        columnResponseList.add(oldForNewColumnResponse);
        //首饰手表
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(45);
        oldForNewColumnResponse.setTitle("首饰手表");
        oldForNewColumnResponse.setProductList(activityService.getList(1,6, 45));
        columnResponseList.add(oldForNewColumnResponse);
        //家纺家具
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(46);
        oldForNewColumnResponse.setTitle("家纺家具");
        oldForNewColumnResponse.setProductList(activityService.getList(1,6, 46));
        columnResponseList.add(oldForNewColumnResponse);
        //3C数码
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(44);
        oldForNewColumnResponse.setTitle("3C数码");
        oldForNewColumnResponse.setProductList(activityService.getList(1,6, 44));
        columnResponseList.add(oldForNewColumnResponse);
        oldForNewIndexResponse.setColumnList(columnResponseList);
        return CommonResult.success(oldForNewIndexResponse);
    }

    @GetMapping("activityCenter")
    public CommonResult activityCenter(@RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort) {
        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        H5OldForNewIndexResponse oldForNewIndexResponse=new H5OldForNewIndexResponse();
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        //轻松换家电
        OldForNewColumnResponse oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(116);
        oldForNewColumnResponse.setTitle("轻松换家电");
        oldForNewColumnResponse.setExtra("家电补贴 立减200元");
        oldForNewColumnResponse.setProductList(activityService.queryOldForNewList(116, 1, 30,activityMin, activityMax,sort));
        columnResponseList.add(oldForNewColumnResponse);
        //超电黑科技
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(117);
        oldForNewColumnResponse.setTitle("潮电黑科技");
        oldForNewColumnResponse.setExtra("追潮电 敢热爱 开启智能新生活");
        oldForNewColumnResponse.setProductList(activityService.queryOldForNewList(117, 1, 30,activityMin, activityMax,sort));
        columnResponseList.add(oldForNewColumnResponse);
        //探索新产品
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(118);
        oldForNewColumnResponse.setTitle("探索新产品");
        oldForNewColumnResponse.setExtra("科学改变生活走在技术前沿");
        oldForNewColumnResponse.setProductList(activityService.queryOldForNewList(118, 1, 30,activityMin, activityMax,sort));
        columnResponseList.add(oldForNewColumnResponse);
        //新款好家电
        oldForNewColumnResponse=new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(119);
        oldForNewColumnResponse.setTitle("生活好家电");
        oldForNewColumnResponse.setExtra("一款好家电让生活更有品质");
        oldForNewColumnResponse.setProductList(activityService.queryOldForNewList(119, 1, 30,activityMin, activityMax,sort));
        columnResponseList.add(oldForNewColumnResponse);
        oldForNewIndexResponse.setColumnList(columnResponseList);
        return CommonResult.success(oldForNewIndexResponse);
    }

    /**
     * 以旧换新列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewListByPage")
    public CommonResult queryOldForNewListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                                 @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort) {

        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(499);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(999);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1999);
            activityMax=new BigDecimal(2000);
        }
        List<OldForNewProductDto> productList = null;
        productList = activityService.queryOldForNewList(columnId, pageNum, pageSize,activityMin, activityMax,sort);

        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();

        productList = productList.stream()
                .map(i -> {
                    Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                    if (subsidyHash != null && subsidyHash.size() > 0) {
                        Object subsidyData = subsidyHash.get(i.getId().toString());
                        if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                            i.setIsReceiveSubsidy(1);
                        }else {
                            i.setIsReceiveSubsidy(0);
                        }
                    }
                    return i;
                })
                .collect(Collectors.toList());
        return CommonResult.success(productList);
    }

    /**
     * 以旧换新栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewColumnList")
    public CommonResult queryOldForNewColumnList(@RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort) {
        OldForNewColumnProductListResponse response=new OldForNewColumnProductListResponse();
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(499);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(999);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1999);
            activityMax=new BigDecimal(2000);
        }
        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();
        String platformData = TokenUtil.getPlatformData();
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.OLD_FOR_NEW.name() + TokenUtil.getDataType() + "_H5_" + flag;
        String s = redisService.get(key);
        if (StringUtils.isNotEmpty(s)) {
            response=JSONObject.parseObject(s,OldForNewColumnProductListResponse.class);
            response.setColumnList(response.getColumnList().stream().map(item -> {
                //14---超值补贴处理
//                if(14==item.getId()){
//                    List<OldForNewProductDto> productList=item.getProductList();
//                    List<OldForNewProductDto> newProductList=new ArrayList<>();
//                    //前10个一组，后面一组
//                    if(productList.size()>10) {
//                        List<OldForNewProductDto> topProducts = productList.subList(0, 10);
//                        List<OldForNewProductDto> otherList = productList.subList(10,productList.size());
//                        Collections.shuffle(topProducts);
//                        Collections.shuffle(otherList);
//                        newProductList.addAll(topProducts);
//                        newProductList.addAll(otherList);
//                        item.setProductList(newProductList);
//                    }else{
//                        item.setProductList(productList);
//                    }
//                }else {
//                    Collections.shuffle(item.getProductList());
//                }
                if(1==sort){
                    Collections.shuffle(item.getProductList());
                }
                return item;
            }).collect(Collectors.toList()));
            return CommonResult.success(response);
        }else {
            List<OldForNewProductDto> productList = null;
            //14---超值补贴
            productList = activityService.queryOldForNewList(14, 1, 100, activityMin, activityMax,sort);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            OldForNewColumnResponse oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(14);
            oldForNewColumnResponse.setTitle("超值补贴");
//            //前10个一组，后面一组
//            if(productList.size()>10) {
//                List<OldForNewProductDto> newProductList=new ArrayList<>();
//                List<OldForNewProductDto> topProducts = productList.subList(0, 10);
//                List<OldForNewProductDto> otherList = productList.subList(10,productList.size());
//
//                Collections.shuffle(topProducts);
//                Collections.shuffle(otherList);
//                newProductList.addAll(topProducts);
//                newProductList.addAll(otherList);
//                oldForNewColumnResponse.setProductList(newProductList);
//            }else{
//                oldForNewColumnResponse.setProductList(productList);
//            }
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            //37---大额补贴
            productList = activityService.queryOldForNewList(37, 1, 100, activityMin, activityMax,sort);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(37);
            oldForNewColumnResponse.setTitle("大额补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            //47--品牌补贴
            productList = activityService.queryOldForNewList(47, 1, 100, activityMin, activityMax,sort);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(47);
            oldForNewColumnResponse.setTitle("品牌补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            //48---换新补贴
            productList = activityService.queryOldForNewList(48, 1, 100, activityMin, activityMax,sort);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(48);
            oldForNewColumnResponse.setTitle("换新补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            response.setColumnList(columnResponseList);

            redisService.set(key, JSONObject.toJSONString(response));
            redisService.expire(key, 60);
        }
        return CommonResult.success(response);
    }

    /**
     * 补贴中心
     * @return
     */
    @GetMapping("/queryOldForNewCenter")
    public CommonResult queryOldForNewCenter() {
        OldForNewColumnProductListResponse response=new OldForNewColumnProductListResponse();
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);

        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.OLD_FOR_NEW.name() + TokenUtil.getDataType() + "_H5_center";
        String s = redisService.get(key);
        if (StringUtils.isNotEmpty(s)) {
            response=JSONObject.parseObject(s,OldForNewColumnProductListResponse.class);
//            response.setColumnList(response.getColumnList().stream().map(item -> {
//                Collections.shuffle(item.getProductList());
//                return item;
//            }).collect(Collectors.toList()));
            return CommonResult.success(response);
        }else {
            List<OldForNewProductDto> productList = null;
            //15---限时补贴
            productList = activityService.queryOldForNewList(15, 1, 9, activityMin, activityMax,0);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            OldForNewColumnResponse oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(15);
            oldForNewColumnResponse.setTitle("限时补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);

            response.setColumnList(columnResponseList);

            redisService.set(key, JSONObject.toJSONString(response));
            redisService.expire(key, 60);
        }
        return CommonResult.success(response);
    }

    /**
     * 补贴中心列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewCenterListByPage")
    public CommonResult queryOldForNewCenterListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                                 @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort) {

        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(0);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(501);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1001);
            activityMax=new BigDecimal(2000);
        }
        List<OldForNewProductDto> productList = null;
        productList = activityService.queryOldForNewList(columnId, pageNum, pageSize,activityMin, activityMax,sort);

        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();

        productList = productList.stream()
                .map(i -> {
                    Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                    if (subsidyHash != null && subsidyHash.size() > 0) {
                        Object subsidyData = subsidyHash.get(i.getId().toString());
                        if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                            i.setIsReceiveSubsidy(1);
                        }else {
                            i.setIsReceiveSubsidy(0);
                        }
                    }
                    return i;
                })
                .collect(Collectors.toList());
        return CommonResult.success(productList);
    }

}
